import {defineStore} from "pinia";
import axios from "axios";

export const useAdminStore = defineStore('admin', {
    state: () => ({
        adminInfo: {},
        loading: false,
        error: null,
    }),
    actions: {
        async login(account, password) {
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/admin/login', {
                    account,
                    password
                });
                if (response.data.code === 200) {
                    console.log(response.data.data)
                    this.adminInfo = response.data.data;
                    return true;
                }

                // 后续可增加token或其它认证信息
            } catch (error) {
                this.error = error.response ? error.response.data.message : '登录失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        // 该函数由于数据表设计还欠缺，暂不完成
        async getUserInfo() {
            try {
                const adminId = this.adminInfo.adminId;
                if (adminId === null) {
                    console.log("管理员ID为空");
                    return false;
                }
                const response = await axios.post('/api/admin/getUserInfo', {adminId: adminId});
                if (response.data.code === 200) {
                    console.log("成功")

                    return true;
                }
            } catch (error) {

            }
            return false;
        }
    },

});
